package fule.com.playandroidkotlin.ui.fragment

import android.app.ActivityOptions
import android.content.Intent
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import fule.com.playandroidkotlin.R
import fule.com.playandroidkotlin.base.BaseFragment
import fule.com.playandroidkotlin.base.Model
import fule.com.playandroidkotlin.ui.activity.WebViewActivity
import fule.com.playandroidkotlin.ui.adapter.ProjectListAdapter
import fule.com.playandroidkotlin.ui.adapter.ProjectTabAdapter
import fule.com.playandroidkotlin.ui.model.ProjectListModel
import fule.com.playandroidkotlin.ui.model.ProjectTitleModel
import fule.com.playandroidkotlin.ui.presenter.ProjectPresenter
import fule.com.playandroidkotlin.ui.util.AppConstant
import fule.com.playandroidkotlin.ui.view.ProjectView
import fule.com.playandroidkotlin.ui.weight.DividerDecoration
import kotlinx.android.synthetic.main.fm_mine.*
import kotlinx.android.synthetic.main.toolbar.*

/**
 * 作者： njb
 * 时间： 2018/8/7 0007-下午 5:58
 * 描述： 项目
 * 来源：
 */
class ProjectFragment : BaseFragment<ProjectPresenter>(), (ProjectView) {
    override fun setTitle() {
        toolbar_title!!.text="项目"
    }

    private var projectTabAdapter: ProjectTabAdapter? = null
    private var projectListAdapter: ProjectListAdapter? = null
    private var list: List<ProjectTitleModel.DataBean>? = null
    private var cid: Int = 0
    private var mList: List<ProjectListModel.DatasBean>? = null
    private var page = 1

    override fun getLayoutId(): Int {
        return R.layout.fm_mine
    }

    override fun loadData() {
        presenter!!.projectTree()
    }

    override fun initView() {
        initTabAdapter()
        initListAdapter()
    }


    private fun initTabAdapter() {
        projectTabAdapter.run {
            projectTabAdapter = ProjectTabAdapter(null)
            var manager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
            rv_tab.layoutManager = manager
        }

        rv_tab.run {
            rv_tab.addItemDecoration(DividerDecoration(ContextCompat.getColor(context,R.color.ce7e7e7), 1))
            rv_tab.adapter = projectTabAdapter
        }
    }

    private fun initListAdapter() {
        projectListAdapter = ProjectListAdapter(null)
        projectListAdapter.run {
            var manager = LinearLayoutManager(context)
            rv_project_list.layoutManager = manager
/*            projectListAdapter!!.bindToRecyclerView(rv_project_list)
            projectListAdapter!!.setEmptyView(R.layout.empty_layout_no_data)*/
        }

        rv_project_list.run {
            rv_project_list.adapter = projectListAdapter
        }
    }


    override fun addListener() {
        projectTabAdapter!!.onItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
            list = projectTabAdapter!!.data
            for (m in list!!) {
                m.isSelect = false
            }
            list!![position].isSelect = true
            cid = list!![position].id
            projectTabAdapter!!.notifyDataSetChanged()
            page = 0
            presenter!!.projectList(page, cid)
        }

        projectListAdapter!!.setOnItemClickListener { _, View, position ->
            val options: ActivityOptions = ActivityOptions.makeScaleUpAnimation(View,
                    View.width / 2,
                    View.height / 2,
                    0,
                    0)
            Intent(context, WebViewActivity::class.java).run {
                putExtra(AppConstant.WEBVIEW_URL_KEY, mList!![position].link)
                putExtra(AppConstant.WEBVIEW_TITLE_KEY, mList!![position].title)
                putExtra(AppConstant.WEBVIEW_ID_KEY, mList!![position].id)
                startActivity(this,options.toBundle())
            }
        }
    }

    override fun createPresenter(): ProjectPresenter {
        return ProjectPresenter(this)
    }

    override fun showLoading() {
    }

    override fun onErrorCode(model: Model) {
        super.onErrorCode(model)
        rf_project.finishLoadMore(300)
        rf_project.finishRefresh(300)
    }

    override fun onLoadProjectTree(data: List<ProjectTitleModel.DataBean>) {
        projectTabAdapter!!.setNewData(data)
        data[0].isSelect = true
        cid = data[0].id
        presenter!!.projectList(page, cid)
    }

    override fun onLoadProjectList(data: ProjectListModel) {
        mList = data.datas
        if (page == 0) {
            projectListAdapter!!.setNewData(data.datas)
        } else {
            projectListAdapter!!.addData(data.datas!!)
        }
        rf_project.finishRefresh(300)
        rf_project.finishLoadMore(300)
    }

}
